package com.voxel.sense.infomsg.module.firm.details.video;

import android.text.TextUtils;

import com.example.com.common.app.BaseRecyclerPresenter;
import com.example.com.common.factory.data.DataSource;
import com.voxel.sense.infomsg.module.firm.details.FirmDtlInfoListContract;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import cyy.example.com.facotry.constants.EventBusConstants;
import cyy.example.com.facotry.model.EventEntity;
import cyy.example.com.facotry.model.api.BaseEntity;
import cyy.example.com.facotry.model.api.response.VideoInfoEntity;
import cyy.example.com.facotry.net.helper.FirmNetHelper;

/**
 * Created by ChenYiYao on 2018/3/2.
 */

public class FirmVideoInfoPresenter extends BaseRecyclerPresenter<FirmDtlInfoListContract.View>
        implements FirmDtlInfoListContract.Presenter, DataSource.Callback<BaseEntity<VideoInfoEntity>> {


    public FirmVideoInfoPresenter(FirmDtlInfoListContract.View view) {
        super(view);
    }

    @Override
    public void start() {
        super.start();
        EventBus.getDefault().register(this);
    }

    @Override
    public void destroy() {
        super.destroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void doLoadData(String id) {
        mView.showLoading("");
        FirmNetHelper.getVideoUrl(id, this);

    }

    @Override
    public void onDataNotAvailable(String strRes, int reqId) {
        if (mView == null)
            return;

        mView.onAdapterDataChanged();
        mView.showEmptyView(strRes, true);
    }

    @Override
    public void onDataLoaded(BaseEntity<VideoInfoEntity> data, int reqId) {
        if (mView == null)
            return;

        if (data != null && data.isSuccess()&&data.getData()!=null) {
            VideoInfoEntity body = data.getData();
            if (body.getBody() != null && body.getBody().size() > 0) {
                List<VideoInfoEntity.institutionWatchsiteVoBean> videoDatas = body.getBody();
                for (int i = videoDatas.size() - 1; i >= 0; i--) {
                    VideoInfoEntity.institutionWatchsiteVoBean bean = videoDatas.get(i);
                    if (TextUtils.isEmpty(bean.getVideoAddrH()) && TextUtils.isEmpty(bean.getVideoAddrR())) {
                        videoDatas.remove(bean);
                    }
                }

                RefreshData(body.getBody());
            } else {
                mView.onAdapterDataChanged();
            }
        } else {
            mView.onAdapterDataChanged();
            mView.showEmptyView("暂无数据", false);
        }
    }

    @Subscribe
    public void onEventBus(EventEntity event) {
       if (EventBusConstants.LOAD_FIRM_DATA_FAILURE.equals(event.key)) {
            if (mView != null)
                mView.showError(String.valueOf(event.value));
        }
    }


}
